// 考勤记录类型定义
export interface Attendance {
  id: string;           // 唯一标识
  date: string;         // 考勤日期
  studentId: string;    // 学生ID
  studentName: string;  // 学生姓名
  classId: string;      // 班级ID
  className: string;    // 班级名称
  status: string;       // 考勤状态: present(出勤), absent(缺勤), late(迟到)
  remark?: string;      // 备注
  createdBy?: string;   // 创建人
  createdAt?: string;   // 创建时间
  updatedBy?: string;   // 更新人
  updatedAt?: string;   // 更新时间
}

// 考勤状态选项
export const statusOptions = [
  { value: 'present', label: '出勤', color: 'green' },
  { value: 'absent', label: '缺勤', color: 'red' },
  { value: 'late', label: '迟到', color: 'orange' },
];

// 班级选项（从班级模块获取）
export const classOptions = [
  { value: 'class-101', label: '高一(1)班' },
  { value: 'class-102', label: '高一(2)班' },
  { value: 'class-103', label: '高一(3)班' },
  { value: 'class-104', label: '高一(4)班' },
  { value: 'class-201', label: '高二(1)班' },
  { value: 'class-202', label: '高二(2)班' },
  { value: 'class-203', label: '高二(3)班' },
  { value: 'class-204', label: '高二(4)班' },
  { value: 'class-301', label: '高三(1)班' },
  { value: 'class-302', label: '高三(2)班' },
  { value: 'class-303', label: '高三(3)班' },
  { value: 'class-304', label: '高三(4)班' },
];

// 学生选项
export const studentOptions = [
  { value: 'student-001', label: '张三', classId: 'class-101' },
  { value: 'student-002', label: '李四', classId: 'class-101' },
  { value: 'student-003', label: '王五', classId: 'class-102' },
  { value: 'student-004', label: '赵六', classId: 'class-102' },
  { value: 'student-005', label: '陈七', classId: 'class-103' },
  { value: 'student-006', label: '刘八', classId: 'class-103' },
  { value: 'student-007', label: '周九', classId: 'class-104' },
  { value: 'student-008', label: '吴十', classId: 'class-104' },
  { value: 'student-009', label: '郑十一', classId: 'class-201' },
  { value: 'student-010', label: '王十二', classId: 'class-201' },
  { value: 'student-011', label: '冯十三', classId: 'class-202' },
  { value: 'student-012', label: '陈十四', classId: 'class-202' },
];

// 根据学生ID获取学生信息
const getStudentInfo = (studentId: string) => {
  const student = studentOptions.find(s => s.value === studentId);
  return student || { label: '未知学生', classId: '' };
};

// 根据班级ID获取班级名称
const getClassName = (classId: string) => {
  const cls = classOptions.find(c => c.value === classId);
  return cls?.label || '未知班级';
};

// 生成初始考勤数据
const generateInitialAttendances = (): Attendance[] => {
  const today = new Date();
  const attendances: Attendance[] = [];
  const statusValues = ['present', 'absent', 'late'];
  
  // 为每个学生生成今天、昨天和前天的考勤记录
  for (let dayOffset = 0; dayOffset < 3; dayOffset++) {
    const date = new Date(today);
    date.setDate(date.getDate() - dayOffset);
    const dateStr = date.toISOString().split('T')[0];
    
    studentOptions.forEach(student => {
      // 随机选择考勤状态
      const randomStatus = statusValues[Math.floor(Math.random() * statusValues.length)];
      
      // 避免同一天同一学生的重复记录
      const exists = attendances.some(
        att => att.date === dateStr && att.studentId === student.value
      );
      
      if (!exists) {
        attendances.push({
          id: `attendance-${dateStr}-${student.value}`,
          date: dateStr,
          studentId: student.value,
          studentName: student.label,
          classId: student.classId,
          className: getClassName(student.classId),
          status: randomStatus,
          remark: randomStatus === 'absent' ? '无故缺勤' : '',
          createdBy: 'system',
          createdAt: new Date().toISOString(),
        });
      }
    });
  }
  
  return attendances;
};

// 初始考勤数据
export const initialAttendances = generateInitialAttendances();

// 验证考勤数据
const validateAttendance = (attendance: Omit<Attendance, 'id'>): { isValid: boolean; errors: string[] } => {
  const errors: string[] = [];
  
  if (!attendance.date) {
    errors.push('考勤日期不能为空');
  }
  
  if (!attendance.studentId) {
    errors.push('学生不能为空');
  }
  
  if (!attendance.classId) {
    errors.push('班级不能为空');
  }
  
  if (!attendance.status) {
    errors.push('考勤状态不能为空');
  }
  
  return {
    isValid: errors.length === 0,
    errors,
  };
};

// 导出验证函数
export { validateAttendance };